import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const USER = uni.getStorageSync('userInfo') || {};
const ACCESSTOKEN = uni.getStorageSync('accessToken') || '';
const GLOBALCONFIG = uni.getStorageSync("CONFIG") || {};

const store = new Vuex.Store({
	state: {
		accessToken: ACCESSTOKEN,
		userInfo: USER,
		globalConfig: GLOBALCONFIG,
		hasLogin: false,
		orderCountTime: 0,
		reloadIndex: false,
	},
	mutations: {
		login(state, provider) {
			state.hasLogin = true;
			state.userInfo = provider.member;
			state.accessToken = provider.member.access_token;
			uni.setStorageSync('accessToken', provider.member.access_token);
			uni.setStorageSync('userInfo', provider.member);
			uni.setStorageSync('parent', provider.member.id);
			uni.setStorageSync('user', provider);
		},
		logout(state) {
			state.hasLogin = false;
			state.accessToken = '';
			state.userInfo = {};
			state.orderCountTime = 0;
			uni.removeStorageSync('user');
			uni.removeStorageSync('accessToken');
			uni.removeStorageSync('userInfo');
			uni.removeStorageSync('USERQRCODE');
			uni.removeStorageSync('HAIBAO');
			uni.removeStorageSync('ORDERCOUNT');
		},
		setOrderCountTime(state){
			state.orderCountTime = Date.parse(new Date()) / 1000;
		},
		setGlobalConfig(state, provider) {
			state.globalConfig = provider
		},
		setReloadIndex(state, provider) {
			state.reloadIndex = provider;
		},
		updateUserInfo(state, provider){
			state.userInfo = provider;
			uni.setStorageSync('userInfo', provider);
		}
	},
	actions: {

	}
})

export default store
